package hit.example.home;

import com.memetix.mst.language.Language;
import com.memetix.mst.translate.Translate;

import hit.example.hitdict.R;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class TranslateFm extends MyFragment implements OnClickListener {
	ProgressDialog pDialog;
	View v;

	public TranslateFm() {
		super();
	}

	String translatedText;
	EditText edtInput;
	TextView txtTranslate;
	Button btnAnhViet, btnVietAnh, btndel;

	public static TranslateFm getInstance() {
		TranslateFm tran = new TranslateFm();
		return tran;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		v = inflater.inflate(R.layout.fm_translate, container, false);
		edtInput = (EditText) v.findViewById(R.id.calc_txt_Prise);
		btndel = (Button) v.findViewById(R.id.calc_clear_txt_Prise);
		btndel.setVisibility(View.GONE);

		txtTranslate = (TextView) v.findViewById(R.id.txtTranlate);
		txtTranslate.setVisibility(View.GONE);
		btnAnhViet = (Button) v.findViewById(R.id.idbtnAnhViet);
		btnVietAnh = (Button) v.findViewById(R.id.idbtnVietAnh);
		btnAnhViet.setVisibility(View.GONE);
		btnVietAnh.setVisibility(View.GONE);
		HomeActivity.getInstance().setHide();
		HomeActivity.getInstance().changeTitle("Translate");
		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		btnAnhViet.setOnClickListener(this);
		btnVietAnh.setOnClickListener(this);
		edtInput.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int length, int arg2,
					int arg3) {
				if (length >= 1) {

					btndel.setVisibility(View.VISIBLE);
					btnAnhViet.setVisibility(View.VISIBLE);
					btnVietAnh.setVisibility(View.VISIBLE);
				}

			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub

			}
		});
		btndel.setOnClickListener(this);
	}

	class TransAsyntask extends AsyncTask<Language, Void, Void> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(getActivity());
			pDialog.setMessage("Please wait...");
			pDialog.setCancelable(true);
			pDialog.show();

		}

		protected Void doInBackground(Language... text) {

			Translate.setClientId("MicrosoftTranslatorJavaAPI");
			Translate
					.setClientSecret("0VHbhXQnJrZ7OwVqcoX/PDZlyLJS9co3cVev1TPr8iM=");
			try {
				translatedText = Translate.execute(edtInput.getText()
						.toString(), text[0], text[1]);
			} catch (Exception e) {
				translatedText = e.toString();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			if (pDialog.isShowing())
				pDialog.dismiss();
			txtTranslate.setVisibility(View.VISIBLE);
			txtTranslate.setText(translatedText.toString());
		}

	}

	@Override
	public boolean onBackPressed() {
		HomeActivity.getInstance().setUptitle("English - Vietnamese");
		HomeActivity.getInstance().setShow();
		HomeActivity.getInstance().pusListWordFm();

		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.idbtnAnhViet:

			new TransAsyntask().execute(Language.ENGLISH, Language.VIETNAMESE);
			break;

		case R.id.idbtnVietAnh:
			new TransAsyntask().execute(Language.VIETNAMESE, Language.ENGLISH);
			break;
		case R.id.calc_clear_txt_Prise:
			edtInput.setText("");
			txtTranslate.setVisibility(View.GONE);
			btnAnhViet.setVisibility(View.GONE);
			btnVietAnh.setVisibility(View.GONE);
			break;
		}

	}
}
